package com.yonyou.brigade.cron.order.ystrip;

/**
 * 云上火车票定时同步任务
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.ystrip.model.YStripSyncOrderParam;
import com.yonyou.brigade.third_parties.ystrip.service.IYStripPublicService;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.List;

@ElasticJobConfig(cron = "0 0 2 * * ? ", shardingTotalCount = 1)
public class FetchYStripTrainOrders implements SimpleJob {

    private static Logger logger = LoggerFactory.getLogger(FetchYStripTrainOrders.class);

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("YStripPublicServiceImpl")
    private IYStripPublicService iyStripPublicService;

    @Override
    public void execute(ShardingContext shardingContext) {
        logger.info("<<<<ystrip-pull-orders-begin>>>>");
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel("ystrip");
        if (briChannels != null && briChannels.size() > 0){
            briChannels.forEach(briChannel -> {
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                String begin = DateUtils.getCurrentDateMinusOneDay();
                String end = DateUtils.getCurrentDateMinusOneDay();
                YStripSyncOrderParam yStripSyncOrderParam = new YStripSyncOrderParam();
                JSONObject configJSON = JSON.parseObject(briChannel.getConfig());
                yStripSyncOrderParam.setCompanyNo(configJSON.getString("companyNo"));
                yStripSyncOrderParam.setBeginDate(begin);
                yStripSyncOrderParam.setEndDate(end);
                try {
                    iyStripPublicService.syncOrders(yStripSyncOrderParam,briChannel.getId(),tenant);
                }catch (Exception e){
                    logger.error(e.getMessage(),e);
                }
            });
        }
        logger.info("<<<<ystrip-pull-orders-end>>>>");
    }
}
